package com.seleniti.enpareja.android.ui.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.seleniti.enpareja.android.R;
import com.seleniti.enpareja.android.ui.widget.UserFormWidget;
import com.seleniti.enpareja.controller.ui.view.UserSettingsView;
import com.seleniti.enpareja.controller.ui.view.UserSettingsView.Action;
import com.seleniti.enpareja.controller.ui.view.UserSettingsView.Event;
import com.seleniti.enpareja.controller.ui.view.UserSettingsView.Operation;
import com.seleniti.enpareja.controller.ui.view.UserSettingsView.Presenter;
import com.seleniti.enpareja.controller.ui.view.UserSettingsViewPresenter;
import com.seleniti.enpareja.controller.ui.widget.UserForm;

public class UserSettingsViewWidget extends EnParejaBaseView<Event, Operation, Action, Presenter> implements UserSettingsView {

	private UserFormWidget userForm;
	private boolean showDownloadAppWhenPassword;
	private Boolean initialPasswordChangeEnabled;

	public UserSettingsViewWidget() {
		super(false);
	}

	public UserSettingsViewWidget(boolean passwordChangeEnabled) {
		this();
		this.setPasswordChangeEnabled(passwordChangeEnabled);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setRetainInstance(true);
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		this.presenter = new UserSettingsViewPresenter(this, eventbus, operationManager, actionController);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.settings_view, container, false);

		this.userForm = (UserFormWidget) view.findViewById(R.id.userForm);

		if (this.initialPasswordChangeEnabled != null) {
			this.userForm.setPasswordChangeEnabled(this.initialPasswordChangeEnabled);
		}

		return view;
	}

	@Override
	public UserForm userForm() {
		return this.userForm;
	}

	@Override
	public void setPasswordChangeEnabled(boolean passwordChangeEnabled) {
		if (this.userForm != null) {
			this.userForm.setPasswordChangeEnabled(passwordChangeEnabled);
		} else {
			this.initialPasswordChangeEnabled = passwordChangeEnabled;
		}
	}

	@Override
	public boolean isPasswordChangeEnabled() {
		return this.userForm.isPasswordChangeEnabled();
	}

	@Override
	public void setShowDownloadAppWhenPassword(boolean showDownloadAppWhenPassword) {
		this.showDownloadAppWhenPassword = showDownloadAppWhenPassword;
	}

	@Override
	public boolean isShowDownloadAppWhenPassword() {
		return this.showDownloadAppWhenPassword;
	}

}
